package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.alibaba.fastjson.JSON;
import com.sxit.tdf.server.entity.FileStorage;
import com.sxit.tdf.server.entity.bo.SimpleStoreBo;
import com.sxit.tdf.server.entity.vo.InsertStoreInfoVo;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IFileStorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 橙子高
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/file-storage")
@Tag(name = "FileStorageController", description = "文件存储接口")
public class FileStorageController {

  @Autowired private IFileStorageService fileStorageService;

  @Operation(summary = "获取文件储存接口")
  @GetMapping("/getStorageList")
  @SaCheckLogin
  @SaCheckPermission("FILE_MEMORY:SELECT")
  public R getStorageList() {
    List<SimpleStoreBo> list = fileStorageService.getStorageList();
    return R.ok().put("data", list);
  }

  @Operation(summary = "插入或修改存储室信息")
  @PostMapping("/insertOrUpdateStoreInfo")
  @SaCheckPermission(
      value = {"FILE_MEMORY:INSERT", "FILE_MEMORY:UPDATE"},
      mode = SaMode.OR)
  public R insertOrUpdateStoreInfo(@Valid @RequestBody InsertStoreInfoVo insertStoreInfoVo) {

    FileStorage fileStorage = new FileStorage();

    if (fileStorage.getId() == null) {
      fileStorage.setStorageName(insertStoreInfoVo.getStorageName());
      fileStorage.setStorageLongitude(insertStoreInfoVo.getStorageLongitude());
      fileStorage.setStorageLatitude(insertStoreInfoVo.getStorageLatitude());
      fileStorage.setStorageAddress(insertStoreInfoVo.getStorageAddress());
      fileStorage.setCreateTime(LocalDateTime.now());
      fileStorage.setDeptId(insertStoreInfoVo.getDeptId());

      boolean save = fileStorageService.save(fileStorage);
      if (save) {
        return R.ok("保存成功");
      } else {
        return R.error("保存失败");
      }
    } else {
      fileStorage.setStorageName(insertStoreInfoVo.getStorageName());
      fileStorage.setId(insertStoreInfoVo.getId());
      fileStorage.setStorageLongitude(insertStoreInfoVo.getStorageLongitude());
      fileStorage.setStorageLatitude(insertStoreInfoVo.getStorageLatitude());
      fileStorage.setStorageAddress(insertStoreInfoVo.getStorageAddress());
      fileStorage.setDeptId(insertStoreInfoVo.getDeptId());

      boolean save = fileStorageService.updateById(fileStorage);
      if (save) {
        return R.ok("修改成功");
      } else {
        return R.error("修改失败");
      }
    }
  }

  @Operation(summary = "删除存储室信息")
  @GetMapping("/deleteStoreInfoById")
  @SaCheckPermission("FILE_MEMORY:DELETE")
  public R deleteStoreInfoById(@RequestParam("id") Integer id) {
    Boolean delete = fileStorageService.deleteStoreInfoById(id);

    if (delete) {
      return R.ok("删除成功");
    } else {
      return R.error("删除失败");
    }
  }

  @Operation(summary = "获取存储室信息")
  @GetMapping("/getStoreInfoById")
  @SaCheckPermission("FILE_MEMORY:SELECT")
  public R getStoreInfoById(@RequestParam("id") Integer id) {

    return R.ok().put("data", fileStorageService.getById(id));
  }

  @Operation(summary = "获取存储室列表")
  @GetMapping("getStorageListByIds")
  @SaCheckPermission("FILE_MEMORY:SELECT")
  public R getStorageListByIds(@RequestParam("ids") String ids) {
    List<Integer> integers = JSON.parseArray(ids, Integer.class);

    List<FileStorage> list = fileStorageService.getStorageListByIds(integers);

    return R.ok().put("data", list);
  }
}
